package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSTransDAO;

public class FaultSMSRepeatSendDAO {
	private IBaseDAO baseDAO;
	private ITransactionDAO tranDAO;
	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}
	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}
	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}
	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	
	/**
	 * 查询当前时间已报警、但是还未解除的故障
	 * @author jiyongtian
	 **/
	public List<Map<String, Object>> queryActiveFault() {
		String table1 = GlobalDBVar.getDBFullName_Conn("Msg_Flt_Happening", "3", null);
		String table2 = GlobalDBVar.getDBFullName_Conn("Sys_FltISM", "1", null);
		StringBuffer strSQL = new StringBuffer();
		strSQL.append("select SysISM_Level as fltLv,vv.DBCode,vv.VehicleNumber,MsgFH_MsgTime as MsgTime,MsgP_FltCode = sf.SysISM_Code,MsgP_SecTypeName = case when SysISM_CodeColorInfo = '无' then '其他' else SysISM_CodeColorInfo end ,'未知' as Position, ");
		strSQL.append("MsgP_FltAbbr = sf.SysISM_ContentCN_Abbr,vv.ESNNumber,isnull(vv.VehicleContact,'') VehicleContact,isnull(vv.VehicleChargedMobile,'') VehicleChargedMobile,MsgP_VclID = vv.VehicleID  ");
		strSQL.append("from V_VehicleInfo vv inner join " + table1 + " mp  on VehicleID = MsgFH_Vcl_ID inner join " + table2 + " sf on  mp.MsgFH_FaultSPN = sf.SysISM_SPN and mp.MsgFH_FaultFMI = sf.SysISM_FMI ");
		strSQL.append("where vv.UsefulLife>=GETDATE() and vv.Tmnl_SoftEdition is not null and vv.Tmnl_SoftEdition<>'' ");
		return this.baseDAO.execQuery(strSQL.toString(), new Object[]{});
	}
	/**
	 * 查询报警的短信内容
	 **/
	public Map<String, Object> querySMSSendRecord(
			Map<String, Object> faultMsg, List<Map<String, Object>> userList) {
		StringBuffer smsSQL =  new StringBuffer();
		smsSQL.append("select distinct SSR_Content from SMS_SendRecord where SSR_MsgCID= "+MapGetter.getString(faultMsg,"")+" and SSR_Type=1 and SSR_CreateType= '1' and SSR_DBName = '" +
			MapGetter.getString(faultMsg, "")+"' and SSR_UsP_ID in ("+new SMSInfoDAO().getSMSUsersID(userList)+")" );
		return baseDAO.execQueryOne(smsSQL.toString(),new Object[] {});
	}
	/**
	 * 查询故障内容
	 **/
	public Map<String, Object> queryFaultAbbr(Map<String, Object> faultMsg) {
		// 二期优化 wangweiye 项目中此方法从未被调用
		String table = GlobalDBVar.getDBFullName_Conn("Sys_FltISM", "1", null);
		StringBuffer strSQL = new StringBuffer();
		strSQL.append("select fi.SysISM_ContentCN_Abbr from " +  table + " AS fi INNER JOIN "+MapGetter.getString(faultMsg, "MsgP_DBName")+".[dbo].Msg_Flt_Content AS mfc ");
		strSQL.append(" ON mfc.MsgFCo_FaultSPN = fi.SysISM_SPN AND mfc.MsgFCo_FaultFMI = fi.SysISM_FMI where mfc.MsgFCo_ID = "+MapGetter.getString(faultMsg,"MsgP_MsgCID"));
		return baseDAO.execQueryOne(strSQL.toString(),new Object[] {});
	}
	/**
	 * 执行SQL
	 * @param sqlList
	 * @param paramList
	 * @return
	 */
	public boolean tranSQLs(List<String> sqlList,List<Object[]> paramList){
		MyConnectDSTransDAO myConnectDSTransDAO=new MyConnectDSTransDAO();
		return myConnectDSTransDAO.batch(sqlList, paramList);
	}
	/**
	 * 查询该故障12小时报警提示短信是否已发送
	 **/
	public Map<String, Object> checkSMSRe(String smsContent,
			Map<String, Object> user) {
		StringBuffer strBuf = new StringBuffer();
		strBuf.append("select COUNT(1) as count from SMS_SendRecord  where SSR_UsP_ID = ? and SSR_UserName = ? and SSR_Content = ? ");
		return this.baseDAO.execQueryOne(strBuf.toString(), new Object[]{
			MapGetter.getString(user, "UserID"),
			MapGetter.getString(user, "UserName"),
			smsContent});
	}

	/*从提示开关表中获取故障提示开关为关的设备号集合20141017wangkai
	 * 3.6.6新增
	 * vclpromptswitch:提示开关表
	 * VclPS_isOpen=0：开关状态为关
	 * VclPS_Type=1：提示开关类型为故障
	 * */
	public List<String> getFLtRePromtSwitch() {
		List<String> FltRePromSwitch = new ArrayList<String>();

		String sql = "select VclPS_VclID from " +GlobalDBVar.getDBFullName_Conn("VclPromptSwitch","1",null)+
				" where VclPS_isOpen=0 and VclPS_Type=1 ";
		List<Map<String, Object>> list = baseDAO
				.execQuery(sql, new Object[] {});
		for (Map<String, Object> map : list) {
			FltRePromSwitch.add(MapGetter.getString(map, "VclPS_VclID"));
		}
		return FltRePromSwitch;
	}

}
